package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.modules.config.AttachmentTypeEnum;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.purchase.bizvo.StrategicProtocolListReqVO;
import com.movitech.mobile.cz.base.purchase.entity.WStrategicProtocol;
import com.movitech.mobile.cz.base.purchase.entity.ext.ExtWStrategicProtocol;
import com.movitech.mobile.cz.base.purchase.mapper.WStrategicProtocolMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtWStrategicProtocolMapper;
import com.movitech.mobile.cz.base.purchase.service.WStrategicProtocolService;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSAttachment;
import com.movitech.mobile.cz.base.sys.service.SAttachmentService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 战略采购协议 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service("wStrategicProtocolService")
public class WStrategicProtocolServiceImpl extends ServiceImpl<WStrategicProtocolMapper, WStrategicProtocol> implements WStrategicProtocolService {
    @Autowired
    private ExtWStrategicProtocolMapper  extWStrategicProtocolMapper;
    @Autowired
    private SAttachmentService sAttachmentService;

    @Override
    public List<ExtWStrategicProtocol> findByEntity(ExtWStrategicProtocol wStrategicProtocol) {
        return extWStrategicProtocolMapper.getEffectByEntity(wStrategicProtocol);
    }

	@Override
	public List<ExtWStrategicProtocol> findPageByEntity(Page<ExtWStrategicProtocol> page, ExtWStrategicProtocol wStrategicProtocol) {
		return extWStrategicProtocolMapper.getEffectPageByEntity(page,wStrategicProtocol);
	}

	@Override
    public List<ExtWStrategicProtocol> findByIds(List<String> ids) {
        Map<String,Object> param = Maps.newHashMapWithExpectedSize(1);
        param.put("ids",ids);
        return extWStrategicProtocolMapper.getByIds(param);
    }

	@Override
	public RestReturnResult searchStrategicProtocolList(StrategicProtocolListReqVO strategicProtocolListReqVO) {
		
		try{
			
			PageCode pageCode = new PageCode(strategicProtocolListReqVO.getPageNumber(), strategicProtocolListReqVO.getPageSize());
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("regionalCompanyId", strategicProtocolListReqVO.getRegionalCompanyId());
			map.put("strategicName", strategicProtocolListReqVO.getStrategicName());
			map.put("strategicCategoryId", strategicProtocolListReqVO.getStrategicCategoryId());
			map.put("supplierName", strategicProtocolListReqVO.getSupplierName());
			map.put("protocolStartTime", strategicProtocolListReqVO.getProtocolStartTime());
			map.put("protocolEndTime", strategicProtocolListReqVO.getProtocolEndTime());
			
			pageCode.setCount(extWStrategicProtocolMapper.searchStrategicProtocolListCount(map));
			
			map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
            map.put("pageSize", pageCode.getPageSize());
            map.put("pageFlag", strategicProtocolListReqVO.getPageFlag());
            
            List<ExtWStrategicProtocol> list = extWStrategicProtocolMapper.searchStrategicProtocolList(map);
            pageCode.setList(list);
            
            return new RestReturnResult(CommonConstants.SUCCESS, pageCode);
			
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
		
	}

	@Override
	public RestReturnResult getWStrategicProtocol(String id) {
		
		try{
			
			if(StringUtils.isNotBlank(id)){
				
				ExtWStrategicProtocol extWStrategicProtocol = extWStrategicProtocolMapper.selectExtById(id);
				
				if(extWStrategicProtocol != null){
					
					List<ExtSAttachment> attachmentList = sAttachmentService.getExtByRelatedIdAndTypeAndSubType(id, AttachmentTypeEnum.ATTACHMENT_TYPE_36.getCode(), null);
					extWStrategicProtocol.setAttachmentList(attachmentList);
					
					return new RestReturnResult(CommonConstants.SUCCESS, extWStrategicProtocol);
					
				}else{
					throw new RuntimeException("战略采购协议不存在", new ServiceThrowable());
				}
				
			}else{
				throw new RuntimeException("传参为空", new ServiceThrowable());
			}
			
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
		
	}

	@Override
	public List<ExtWStrategicProtocol> findWStrategicProtocolByStrategicProtocolSynId(Map<String, Object> mapParams) {
		return extWStrategicProtocolMapper.findWStrategicProtocolByStrategicProtocolSynId(mapParams);
	}

	@Override
	public boolean checkRepeat(WStrategicProtocol wStrategicProtocol) {
    	WStrategicProtocol wStrategicProtocol1 = new WStrategicProtocol();
		wStrategicProtocol1.setSupplierName(wStrategicProtocol.getSupplierName());
		wStrategicProtocol1.setContractCode(wStrategicProtocol.getContractCode());
		List<WStrategicProtocol> wStrategicProtocols = selectList(new EntityWrapper<>(wStrategicProtocol1));
    	if(CollectionUtils.isEmpty(wStrategicProtocols)){
    		return false;
		}else{
			WStrategicProtocol wStrategicProtocolDb = wStrategicProtocols.get(0);
			wStrategicProtocolDb.setContactName(wStrategicProtocol.getContactName());
			wStrategicProtocolDb.setStrategicCategoryName(wStrategicProtocol.getStrategicCategoryName());
			updateById(wStrategicProtocolDb);
		}
		return true;
	}
}
